﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StoragePeriod.Data;
using StoragePeriod.Common;
using StoragePeriod.Web.Common;
using System.Text;

namespace StoragePeriod.Web.SysManage
{
    public partial class PersonnelManage : SecurityPage
    {
        #region 属性

        #endregion

        #region 继承
        public override string PrivCode()
        {
            return "a94c41b35a0bfd43";
        }
        #endregion

        #region 函数
        private void DropBanding()
        {
            List<OperationTeam> OperationList = OperationTeam.Where("ParentId=@0",0).ToList();
            this.Drop_Operation.DataSource = OperationList;
            this.Drop_Operation.DataValueField = "id";
            this.Drop_Operation.DataTextField = "OTName";
            this.Drop_Operation.DataBind();
            this.Drop_Operation_SelectedIndexChanged(null,null);
        }

        private void DropBandingSearch()
        {
            List<OperationTeam> OperationList = OperationTeam.Where("ParentId=@0", 0).OrderByDescending(p=>p.id).ToList();
            this.Drop_OperationSearch.DataSource = OperationList;
            this.Drop_OperationSearch.DataValueField = "id";
            this.Drop_OperationSearch.DataTextField = "OTName";
            this.Drop_OperationSearch.DataBind();
            this.Drop_OperationSearch.Items.Insert(0,new ListItem("全部","-1"));
            this.Drop_OperationSearch_SelectedIndexChanged(null, null);
        }

        private void DataBing()
        {
            using (StoragePeriodDataContext data = new StoragePeriodDataContext())
            {
                var q = from f in data.SYS_Users
                        join A in data.OperationTeam on f.UserType equals A.id
                        select new
                        {
                            f.id,
                            f.UserCode,
                            f.UserPWD,
                            f.UserName,
                            f.RoleCode,
                            f.RoleName,
                            f.UserType,
                            f.AuthType,
                            f.UserStatus,
                            f.EMail,
                            f.Hometown,
                            f.Political,
                            f.Sex,
                            f.EducatedLevel,
                            f.QQ,
                            f.Address,
                            f.BirthDate,
                            f.Specialty,
                            f.Unit,
                            f.IDNumber,
                            f.JobTitle,
                            f.MobileTelNo,
                            f.Descriptions,
                            f.CreatedTime,
                            ParentTemp = A.ParentId
                        };
                q = q.Where(p => p.UserName.Contains(this.txt_searchName.Text.Trim()));

                if (this.Drop_TeamSearch.SelectedValue != "-1" && this.Drop_TeamSearch.SelectedValue != "")
                {
                    q = q.Where(p => p.UserType == int.Parse(this.Drop_TeamSearch.SelectedValue));
                }
                else
                {
                    if (this.Drop_OperationSearch.SelectedValue != "" && this.Drop_OperationSearch.SelectedValue != "-1")
                    {
                        q = q.Where(p => p.ParentTemp == int.Parse(this.Drop_OperationSearch.SelectedValue));
                    }
                }

                this.AspNetPager1.RecordCount = q.Count();
                this.RepDataList.DataSource = q.Skip((this.AspNetPager1.CurrentPageIndex - 1) * this.AspNetPager1.PageSize).Take(this.AspNetPager1.PageSize).ToList();
                this.RepDataList.DataBind();
            }
        }

        int i = 0;
        public string GetXH()
        {
            i += 1;
            return i.ToString();
        }

        public string GetTeamName(string TeamId)
        {
            return OperationTeam.Where("id=@0", int.Parse(TeamId)).First().OTName;
        }
        #endregion

        #region 事件

        protected void Page_Load(object sender, EventArgs e)
        {

            if (IsPostBack) return;
            DropBanding();
            DropBandingSearch();
            DataBing();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            //this.AspNetPager1.CurrentPageIndex += 1;
            this.DataBing();
        }

        protected void RepDataList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                int RawTypeId = Convert.ToInt32(e.CommandArgument);
                SYS_Users md = SYS_Users.Where("id=@0", RawTypeId).First();
                this.HidTypeID.Value = RawTypeId.ToString();
                StringBuilder sb = new StringBuilder();
                sb.Append("'"+md.UserName+"',");
                OperationTeam md_Operation = OperationTeam.Where("id=@0", md.UserType).First();
                sb.Append("'" + md_Operation.ParentId + "',");
                sb.Append("'" + md.UserType + "',");
                sb.Append("'" + md.EMail + "',");
                sb.Append("'" + md.Hometown + "',");
                sb.Append("'" + md.Political + "',");
                sb.Append("'" + md.Sex + "',");
                sb.Append("'" + md.EducatedLevel + "',");
                sb.Append("'" + md.QQ + "',");
                sb.Append("'" + md.Address + "',");
                sb.Append("'" + (md.BirthDate != null ? Convert.ToDateTime(md.BirthDate).ToString("yyyy-MM-dd") : "") + "',");
                sb.Append("'" + md.Specialty + "',");
                sb.Append("'" + md.Unit + "',");
                sb.Append("'" + md.IDNumber + "',");
                sb.Append("'" + md.JobTitle + "',");
                sb.Append("'" + md.MobileTelNo + "',");
                sb.Append("'"+md.Descriptions+"'");
                UtilityUI.Load(this.upPanelMain, "LoadData("+sb.ToString()+");ShowDlg(1)");
            }
            else
            {
                int RawTypeId = Convert.ToInt32(e.CommandArgument);
                SYS_Users.Delete("id=@0", RawTypeId);
                DataBing();
                UtilityUI.Load(this.upPanelMain, "alertstr('删除成功！')");
            }
        }

        protected void btPost_Click(object sender, EventArgs e)
        {
            if (this.HidIsEdit.Value == "0")
            {
                //添加
                SYS_Users md = new SYS_Users();
                md.UserCode = "";
                md.UserPWD = "";
                md.UserName = this.txt_UserName.Text.Trim();
                md.RoleCode = "";
                md.RoleName = "";
                md.UserType = Convert.ToInt32(this.Drop_Team.SelectedValue);
                md.AuthType = "";
                md.UserStatus = "";
                md.EMail = this.txt_Email.Text.Trim();
                md.Hometown = this.txt_Hometown.Text.Trim();
                md.Political = this.txt_Political.Text.Trim();
                md.Sex = this.Drop_Sex.SelectedValue;
                md.EducatedLevel = this.txt_EducatedLevel.Text.Trim();
                md.QQ = this.txt_QQ.Text.Trim();
                md.Address = this.txt_Address.Text.Trim();
                if (this.txt_BirthDate.Text.Trim() != "")
                {
                    md.BirthDate = Convert.ToDateTime(this.txt_BirthDate.Text.Trim());
                }
                md.Specialty = this.txt_Specialty.Text.Trim();
                md.Unit = this.txt_Unit.Text.Trim();
                md.IDNumber = this.txt_IDNumber.Text.Trim();
                md.JobTitle = this.txt_JobTitle.Text.Trim();
                md.MobileTelNo = this.txt_MobileTelNo.Text.Trim();
                md.Descriptions = this.txt_Descriptions.Text.Trim();
                md.CreatedTime = DateTime.Now;
                SYS_Users.Insert(md);
                DataBing();
                UtilityUI.Load(this.UpdatePanel1, "$('#dialog-form').dialog('close');;alertstr('添加成功！')");
            }
            else
            {
                //修改
                int RawTypeId = Convert.ToInt32(this.HidTypeID.Value);
                SYS_Users md = SYS_Users.Where("id=@0", RawTypeId).First();
                md.UserName = this.txt_UserName.Text.Trim();
                md.UserType = Convert.ToInt32(this.Drop_Team.SelectedValue);
                md.EMail = this.txt_Email.Text.Trim();
                md.Hometown = this.txt_Hometown.Text.Trim();
                md.Political = this.txt_Political.Text.Trim();
                md.Sex = this.Drop_Sex.SelectedValue;
                md.EducatedLevel = this.txt_EducatedLevel.Text.Trim();
                md.QQ = this.txt_QQ.Text.Trim();
                md.Address = this.txt_Address.Text.Trim();
                if (this.txt_BirthDate.Text.Trim() != "")
                {
                    md.BirthDate = Convert.ToDateTime(this.txt_BirthDate.Text.Trim());
                }
                md.Specialty = this.txt_Specialty.Text.Trim();
                md.Unit = this.txt_Unit.Text.Trim();
                md.IDNumber = this.txt_IDNumber.Text.Trim();
                md.JobTitle = this.txt_JobTitle.Text.Trim();
                md.MobileTelNo = this.txt_MobileTelNo.Text.Trim();
                md.Descriptions = this.txt_Descriptions.Text.Trim();
                SYS_Users.Update(md, "id=@0", RawTypeId);
                DataBing();
                UtilityUI.Load(this.UpdatePanel1, "$('#dialog-form').dialog('close');;alertstr('修改成功！')");
            }
        }

        protected void Drop_Operation_SelectedIndexChanged(object o, EventArgs e)
        {
            if (this.Drop_Operation.SelectedItem != null)
            {
                int ParentId = Convert.ToInt32(this.Drop_Operation.SelectedValue);
                List<OperationTeam> TeamList = OperationTeam.Where("ParentId=@0", ParentId).ToList();
                this.Drop_Team.DataSource = TeamList;
                this.Drop_Team.DataValueField = "id";
                this.Drop_Team.DataTextField = "OTName";
                this.Drop_Team.DataBind();
               
            }
        }

        protected void Drop_OperationSearch_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.Drop_OperationSearch.SelectedItem != null)
            {
                int ParentId = Convert.ToInt32(this.Drop_OperationSearch.SelectedValue);
                List<OperationTeam> TeamList;
                if (ParentId == -1)
                {
                    TeamList = new List<OperationTeam>();
                }
                else
                {
                    TeamList = OperationTeam.Where("ParentId=@0", ParentId).ToList();
                }
                TeamList.Insert(0, new OperationTeam() { id = -1, OTName = "全部" });
                this.Drop_TeamSearch.DataSource = TeamList;
                this.Drop_TeamSearch.DataValueField = "id";
                this.Drop_TeamSearch.DataTextField = "OTName";
                this.Drop_TeamSearch.DataBind();
            }
        }

        protected void Btn_Search_Click(object sender, EventArgs e)
        {
            DataBing();
        }

        #endregion

        
    }
}